package com.housediyadmin.view.components
{
	/**
	 * 
	 * @author wenjun cai
	 * 2010/11/24
	 */	
	import com.housediyadmin.events.treeEvents.DeleteAreaLevelEvent;
	import com.housediyadmin.events.treeEvents.EditAreaLevelEvent;
	import com.housediyadmin.events.treeEvents.EditGoodsLevelEvent;
	import com.housediyadmin.events.treeEvents.GetAreaTreeEvent;
	import com.housediyadmin.events.treeEvents.GetGoodsTreeEvent;
	import com.housediyadmin.model.AreaTreeModel;
	import com.housediyadmin.model.BuildingModel;
	import com.housediyadmin.model.GoodsTreeModel;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Actor;
	import org.robotlegs.mvcs.Mediator;
	
	public class TreeMediator extends Mediator
	{
		[Inject]
		public var view:TreeView;
		
		[Inject]
		public var buildingTreeModel:AreaTreeModel;
		
		[Inject]
		public var goodsTreeModel:GoodsTreeModel;;
		
		//待删，只是做测试
		public function TreeMediator()
		{
			super();
			
		}
		override public function onRegister():void
		{
			switch(view.type)
			{
				case "BUILDING":
					BindingUtils.bindProperty(view.tree, "dataProvider", buildingTreeModel, "areaTreeXML");
					dispatch(new GetAreaTreeEvent(GetAreaTreeEvent.GET_AREA_TREE));
					break;
				case "GOODS":
					BindingUtils.bindProperty(view.tree,"dataProvider",goodsTreeModel,"goodsTreeXML");
					dispatch(new GetGoodsTreeEvent(GetGoodsTreeEvent.GET_GOODS_TREE));
				    view.addTypeBtn.addEventListener(MouseEvent.CLICK, addTpye);
					break;
			
			}
			
			
			view.expandBtn.addEventListener(MouseEvent.CLICK,expand);
			view.collapseBtn.addEventListener(MouseEvent.CLICK,collapse);
			
			
			view.addBtn.addEventListener(MouseEvent.CLICK,addNode);
			view.modifyBtn.addEventListener(MouseEvent.CLICK,modifyEve);
			
			view.tree.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, doubleClkTreeNode);
			view.tree.addEventListener(ListEvent.ITEM_EDIT_BEGINNING, beginEditTreeNode);		
			
		}
		
		private function modifyEve(event:MouseEvent):void
		{
			modify(view.tree.selectedIndex);
		}
		
		private function doubleClkTreeNode(event:ListEvent):void
		{
			modify(event.rowIndex);
		}
		
		private function modify(rIndex:int):void
		{
			view.tree.editable = true;
			view.tree.addEventListener(ListEvent.ITEM_EDIT_END,endEditTreeNode);
			view.tree.editedItemPosition = {columnIndex:0, rowIndex:rIndex};
		}
		
		private function beginEditTreeNode(event:ListEvent):void
		{
			event.preventDefault();
		}
		private function endEditTreeNode(event:ListEvent):void
		{
			var typeID:int=event.target.selectedItem.@typeID;
			var typeName:String = event.currentTarget.itemEditorInstance.text;			
			
			switch(view.type)
			{
				case "BUILDING":
					dispatch(new EditAreaLevelEvent(EditAreaLevelEvent.EDIT_AREA_LEVEL,typeID,typeName));
					break;
				case "GOODS":
					dispatch(new EditGoodsLevelEvent(EditGoodsLevelEvent.EDIT_GOODS_LEVEL,typeID,typeName));
					break;
			}
			event.preventDefault();
			view.tree.removeEventListener(ListEvent.ITEM_EDIT_END, endEditTreeNode);
			view.tree.destroyItemEditor();
		}
		
		private function expand(event:MouseEvent):void
		{
			for each(var item:XML in view.tree.dataProvider)             
			view.tree.expandChildrenOf(item,true); 
			switch(view.type)
			{
				case "BUILDING":
					buildingTreeModel.selectedNode=null;
					break;
				case "GOODS":
					goodsTreeModel.selectedNode=null;
					break;
			}
			
		}
		private function collapse(event:MouseEvent):void
		{
			view.tree.openItems=[];	
			switch(view.type)
			{
				case "BUILDING":
					buildingTreeModel.selectedNode=null;
					break;
				case "GOODS":
					goodsTreeModel.selectedNode=null;
					break;
			}
		}
		private function addNode(event:MouseEvent):void
		{
			var popup:EditTreeView = new EditTreeView();
			PopUpManager.addPopUp( popup, contextView ); // contextView is defined in Command
			switch(view.type)
			{
				case "BUILDING":
					popup.parameter=XML(buildingTreeModel.selectedNode);
					break;
				case "GOODS":
					popup.parameter=XML(goodsTreeModel.selectedNode);
					break;
			}
			popup.type=view.type;
			mediatorMap.createMediator( popup );
		}
		
		private function addTpye(event:MouseEvent):void
		{
			var popup:PopUpGoodsType = new PopUpGoodsType();
			PopUpManager.addPopUp(popup, contextView);
			PopUpManager.centerPopUp(popup);
			mediatorMap.createMediator( popup );
			
		}
	}
}